package com.spider.admin.controller;

import com.spider.admin.agent.AgentServer;
import com.spider.admin.conf.ResultMessage;
import com.spider.admin.conf.StructVar;
import com.spider.admin.exception.SpiderException;
import com.spider.admin.http.entity.PageResult;
import com.spider.admin.service.agent.AgentConfService;
import com.spider.admin.store.primary.model.AgentConf;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping("/agentContext")
public class AgentServerController {

    @Autowired
    private AgentConfService agentConfService;

    @Autowired
    private AgentServer agentServer;

    /**
     * 生成全局配置
     *
     * @return
     * @throws SpiderException
     */
    @PostMapping("/createGlobalConf")
    public String createGlobalConf() throws SpiderException {
        return agentConfService.createGlobalConfContext();
    }

    /**
     * 生成服务云配置
     *
     * @param serverId
     * @return
     * @throws SpiderException
     */
    @PostMapping("/createServerConf/{serverId}")
    public String createServerConf(@PathVariable String serverId) throws SpiderException {
        return agentConfService.createServerConfContext(serverId);
    }


    /**
     * 生成应用配置
     *
     * @param appId
     * @return
     * @throws SpiderException
     */
    @PostMapping("/createLocationConf/{appId}")
    public String createLocationConf(@PathVariable String appId) throws SpiderException {
        return agentConfService.createAppConfContext(appId);
    }

    @GetMapping("/list")
    public PageResult<AgentConf> queryConfList(
            @RequestParam(required = false) Integer curPage,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam String confType,
            @RequestParam(required = false) String confVersion,
            @RequestParam String belongId,
            @RequestParam(required = false) Date updateTime) throws SpiderException {
        return agentConfService.queryConfList(curPage, pageSize, confType, belongId, confVersion, updateTime);
    }

    @GetMapping("/runList")
    public PageResult<AgentConf> queryRunConfList(
            @RequestParam(required = false) Integer curPage,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam String confType,
            @RequestParam(required = false) String confVersion,
            @RequestParam String belongId,
            @RequestParam(required = false) Date updateTime) throws SpiderException {
        return agentConfService.queryRunConfList(curPage, pageSize, confType, belongId, confVersion, updateTime);
    }

    @GetMapping("/checkRunStatus")
    public boolean checkAgentRunStatus() throws SpiderException {
        return agentServer.checkAgentServerRunStatus();
    }

    @GetMapping("/reloadServer")
    public boolean restartAgentServer() throws SpiderException {
        agentServer.reloadAgent();
        return true;
    }

    @GetMapping("/validateConf/{confId}")
    public boolean validateAgentConf(@PathVariable String confId) throws SpiderException {
        return agentServer.validateAgentConf(confId);
    }

    @DeleteMapping("/delAgentConf/{confId}")
    public boolean delAgentConf(@PathVariable String confId) throws SpiderException {
        int i=agentConfService.deleteAgentConf(confId);
        if(i==0){
            throw new SpiderException(ResultMessage.FAILED.getCode(),"无记录删除");
        }
        return true;
    }

    @PostMapping("/publishAgentConf/{confId}")
    public boolean publishAgentConf(@PathVariable String confId) throws SpiderException {
        int i=agentConfService.updateAgentConfStatus(confId, StructVar.AGENT_CONF_CONTEXT_STATUS_PUBLISHED);
        if(i==0){
            throw new SpiderException(ResultMessage.FAILED.getCode(),"无记录发布");
        }
        return true;
    }

    @PostMapping("/callBackAgentConf/{confId}")
    public boolean callBackAgentConf(@PathVariable String confId) throws SpiderException {
        int i=agentConfService.updateAgentConfStatus(confId, StructVar.AGENT_CONF_CONTEXT_STATUS_UNPUBLISHED);
        if(i==0){
            throw new SpiderException(ResultMessage.FAILED.getCode(),"无记录收回");
        }
        return true;
    }
}
